package com.lvyh.lightframe.autoconfigure.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.UUID;


/**
 * Custom annotation aspect configuration
 *
 * @author lvyh 2021/03/22.
 */
@Aspect
@Configuration
@Order(0)
public class TraceAnotationAspectAutoConfig {

    private final String traceId = "traceId";

    @Around("@annotation(com.lvyh.lightframe.log.anotation.CustomLogTrace)")
    public Object initLogTraceController(ProceedingJoinPoint joinPoint) throws Throwable {
        MDC.put(this.traceId, UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Object object = null;
        try {
            object = joinPoint.proceed();
            return object;
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        } finally {
            MDC.clear();
        }
    }

}
